#!/bin/busybox sh

# Copyright 2020 sbct
# Distributed under the terms of the GNU General Public License v3

# Initial RAM Filesystem for Full Disk Encryption with detached LUKS header.
# If you plan on using this init script, be sure to modify paths accordingly.
# e.g /dev/sdb1, /mnt/head/dick.img etc.
#
# https://notabug.org/sabactani						    

. /functions

# Mount the /proc, /dev and /sys filesystems.
mount -t devtmpfs devtmpfs /dev
mount -t proc none /proc
mount -t sysfs none /sys

# Function that checks if the flash drive containing the LUKS header has been detected.
# Avoids the use of sleep.
chkHead

# Mount flash drive contaning boot partition and LUKS header.
mkdir -p /mnt/head
ebegin "Mounting flash drive"
mount UUID="4a6fa337-0586-4790-86fd-869f3510aeaf" /mnt/head && eend || rescue_shell
# Unlock LUKS partition. 
clear && einfo "Unlock Hard Drive:" && cryptsetup $cryptOpts /dev/sda1 enema || findRoot || rescue_shell
# Scan for LVM vgs and lvs.
ebegin "Scanning for logical volumes"
/sbin/lvm vgscan --mknodes || rescue_shell
/sbin/lvm lvchange -a ly lvmpica || rescue_shell
/sbin/lvm vgscan --mknodes || rescue_shell

# Mount the root filesystem.
ebegin "Mounting the root filesystem"
mount -o ro /dev/mapper/lvmpica-root /mnt/root && eend || rescue_shell
/sbin/cryptsetup luksOpen $cryptOpts2 /dev/sdb1 urine || findData || rescue_shell
ebegin "Scanning for other logical volumes"
/sbin/lvm vgscan --mknodes || rescue_shell
/sbin/lvm lvchange -a ly lvmtoba || rescue_shell
/sbin/lvm vgscan --mknodes && eend || rescue_shell
# Clean up.
umount /dev
umount /proc
umount /sys
umount /mnt/head

# Boot the real thing.
exec switch_root /mnt/root /sbin/init
